<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Custom;
use App\Http\Requests\CustomStoreRequest;
use App\Http\Requests\CustomUpdateRequest;
use App\Http\ErrCode;
class CustomController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $columns = ['客户名称','客户等级','联系电话','客户经理'];
        return $this->index_model_list(Custom::class,$columns,$request,function($builder) use($request){

            $keyword = $request->get('keyword','');
            $status = $request->get('status',0);

            $builder->with('user','type')->where(['status'=>$status]);
            //自定义查询条件
            if(!empty($keyword)){
                $builder->where(function($q) use($keyword){
                    $q->where('name','like','%'.$keyword.'%')
                      ->orWhere('phone','like','%'.$keyword.'%');
                });
            }
        },function($model) {
            //导出字段定义
            return [
               $model->name,
               $model->type != null ? $model->type->value : '',
               $model->phone,
               $model->user->name
           ];
        });
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CustomStoreRequest $request)
    {
        $data = array_null_to_empty($request->only([
            'name','region_id','address','custom_type','user_id',
            'happy','status','credit','phone','fax','zipcode',
            'website','biz_licence','boss','regist_capital',
            'bank_name','bank_no'
        ]));

        //检测是否已经存在
        $exist_cond = [
            'name' => $data['name']
        ];
        if($this->modelExist(Custom::class,$exist_cond)){
            return json_error(ErrCode::RESOURCE_EXIST,'数据已经存在');
        }

        $model = new Custom();
        $model->fill($data);
        $model->save();
        return response()->json(json_success(['id'=>$model->id]));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = Custom::with('user','type')->findOrFail($id);
        return response()->json(json_success($model));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CustomUpdateRequest $request, $id)
    {
        $data = array_filter($request->only([
            'name','region_id','address','custom_type','user_id',
            'happy','status','credit','phone','fax','zipcode',
            'website','biz_licence','boss','regist_capital',
            'bank_name','bank_no'
        ]));

        $model = Custom::findOrFail($id);

        //检测是否已经存在
        if($this->modelExist(Custom::class,$data,$model->id)){
            return json_error(ErrCode::RESOURCE_EXIST,'数据已经存在');
        }

        $model->update($data);
        return response()->json(json_success());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = Custom::findOrFail($id);
        $model->delete();
        return response()->json(json_success());
    }
}
